package graphSample;

import java.util.HashMap;

import core.Entity;
import core.Graph;
import core.Process;

public class Philosophers {
	static public Graph getGraph(){
		Graph g = new Graph("Philosophers");
		
		// Entities
		Entity e1 = new Entity(g, "Starving");
		e1.addVariable("$pId","INT");
		e1.addVariable("$tFTime", "DOUBLE");
		
		Entity e2 = new Entity(g, "Get_One");
		e2.addVariable("$pId","INT");
		e2.addVariable("$aTime", "DOUBLE");
		
		Entity e3 = new Entity(g, "Eating");
		e3.addVariable("$pId","INT");
		e3.addVariable("$fTime", "DOUBLE");
		
		Entity e4 = new Entity(g, "Drop_Left");
		e4.addVariable("$pId","INT");
		e4.addVariable("$aTime", "DOUBLE");
	
		Entity e5 = new Entity(g, "Chops");
		e5.addVariable("$cId","INT");
		e5.addVariable("$freeTime", "DOUBLE");
		
		Entity e6 = new Entity(g, "Shared_Chops");
		e6.addVariable("$sCId","INT");
		e6.addVariable("$uTime", "DOUBLE");
		
		Process p1 = new Process(g, "getFirst", "Mod[$pId,10000]=$cId AND $status=0 AND $tFTime>= $freeTime OR Mod[$pId+1,10000]=$cId AND $status=0 AND $tFTime>= $freeTime");
		p1.addEntityIn(e1);p1.addEntityIn(e5); 
		p1.addEntityOut(e2);p1.addEntityOut(e6);
		p1.addEvalItem("$sCId=$cId");
		p1.addEvalItem("$uTime=Max[{$freeTime,$tFTime}]");
		
		Process p2 = new Process(g, "getSecond", "Mod[$pId,10000] = $cId AND Mod[$pId+1,10000] = $sCId OR Mod[$pId+1,10000] = $cId AND Mod[$pId,10000] =$CId");//$pid3+1 = $pid5 AND $status = 1 查询时间？
		p2.addEntityIn(e2); p2.addEntityIn(e5); p2.addEntityIn(e6);
		p2.addEntityOut(e3);
		p1.addEvalItem("$fTime=Max[{$freeTime,$uTime}]");
		
		Process p3 = new Process(g, "dropLeft", "TRUE");
		p3.addEntityIn(e3); 
		p3.addEntityOut(e4); p3.addEntityOut(e5);
		p3.addEvalItem("$cId=$pId");
		
		Process p4 = new Process(g, "dropRight", "TRUE");
		p4.addEntityIn(e4);
		p4.addEntityOut(e1); p4.addEntityOut(e5);
		p4.addEvalItem("$cId=$pId+1");

		//Add token in e1
		HashMap<String,String> token;
		for(int i=1;i<=10000;i++){
			token = new HashMap<String,String>();
			token.put("$pId", String.valueOf(i));
			e1.addToken(token);
		}
		
		for(int i=1;i<=10000;i++){
			token = new HashMap<String,String>();
			token.put("$cId", String.valueOf(i));
			e5.addToken(token);
		}
		
		return g;
	}
}

